﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Eugenhome.Atrium.FotoSlider.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SliderView : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string galUrlPath = Request.QueryString["gal"];
            string height = Request.QueryString["h"];
            string width = Request.QueryString["w"];

            if (galUrlPath == null) galUrlPath = "SliderImages";

            string galPhysPath = HttpContext.Current.Server.MapPath(galUrlPath);

            if (Directory.Exists(galPhysPath))
            {
                string[] fotos = Directory.GetFiles(galPhysPath, "*.jpg");

                string timeFak = DateTime.Now.Ticks.ToString();
                Random rnd = new Random(Convert.ToInt32(timeFak.Substring(timeFak.Length - 3, 3)));
                int index = rnd.Next(0, fotos.Length );

                string fullUrl=galUrlPath + "/" + Path.GetFileName(fotos[index]);

                img.BorderWidth = 0;
                img.ImageUrl = fullUrl;

                if (height != null && !height.Equals(string.Empty))
                {
                    img.Height = new Unit(height);
                }
                if (width != null && !width.Equals(string.Empty))
                {
                    img.Width = new Unit(width);
                }

                //Response.Clear();
                //Response.Redirect(fullUrl, true);
            }
        }
    }
}